package com.t2c.tibetantochinese.wordstudy;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseFragment;
import com.t2c.tibetantochinese.home.HomeFragment;
import com.t2c.tibetantochinese.realmModel.Phrase;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.views.TitleBar;

import java.io.IOException;

/**
 * Created by zoulu on 16/6/21.
 * 藏文单词学习播放 页面
 */
public class TibetanWordFragment extends BaseFragment{
    private Phrase phrase;
    private TextView tvTitle;
    private TextView tvVoice;
    private ImageView imageVoice;
    private VideoView videoview;
    private TextView bihua;
    private TextView tvNote;
    private TextView tvNotecontent;
    private Uri uri ;
    private MediaPlayer mediaPlayer ;
    @Override
    protected int getContentViewResId() {
        return R.layout.tibetanword_layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initData();
    }

    private void init(View view) {
        mediaPlayer = new MediaPlayer();
        phrase = (Phrase) getArguments().getSerializable("PHRASE");
        tvTitle = (TextView) view.findViewById( R.id.tv_title1 );
        tvVoice = (TextView) view.findViewById( R.id.tv_voice );
        imageVoice = (ImageView) view.findViewById( R.id.image_voice );
        videoview = (VideoView) view.findViewById( R.id.videoview );
        bihua = (TextView) view.findViewById( R.id.bihua );
        tvNote = (TextView) view.findViewById( R.id.tv_note );
        tvNotecontent = (TextView) view.findViewById( R.id.tv_notecontent );
        TitleBar titleBar = (TitleBar) view.findViewById(R.id.titlebar);
        if(CommonUtil.isChinese(getActivity())) {
            titleBar.setTitle(getResources().getString(R.string.study));
            tvTitle.setText(getResources().getString(R.string.voiceandstudy));
            tvNote.setText(getResources().getString(R.string.comment));
        }else{
            titleBar.getTitle().setTypeface(CommonUtil.getTibetanType(getActivity()));
            titleBar.setTitle(getResources().getString(R.string.tstudy));
            tvTitle.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tvTitle.setText(getResources().getString(R.string.tvoiceandstudy));
            tvNote.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tvNote.setText(getResources().getString(R.string.tcomment));
        }
        titleBar.setOnLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeFragment homeFragment = CommonUtil.getHomeFragment(getActivity());
                homeFragment.fragmentBack();
            }
        });

    }

    private void initData(){
        playVideo(phrase.getVideo().getVideopath(),getContext());
        imageVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio(phrase.getAudio().getTibetaudiopath());
            }
        });
        tvVoice.setTypeface(CommonUtil.getTibetanType(getActivity()));
        tvNotecontent.setTypeface(CommonUtil.getTibetanType(getActivity()));
        tvVoice.setText(phrase.getTibetcontent());
        tvNotecontent.setText(phrase.getComment().getContent() + "\n" + phrase.getComment().getTibetcontent());
    }

    /**
     * 循环播放视频，并且不带播放控件
     * @param videopath 播放地址
     */
    private void playVideo(String videopath,Context context){
        Uri uri = CommonUtil.getVideoPath(videopath, context);
        videoview.setVideoURI(uri);
        videoview.start();
        videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });
    }

    private void playAudio(String path){
        try {
            mediaPlayer.reset();
            AssetFileDescriptor descriptor = CommonUtil.getAudioPath(getContext(),path);
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
